package sicnu.cs.ich.common.common.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import sicnu.cs.ich.api.common.enums.ConfigEnum;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author CaiKe
 * @date 2021/12/4 17:55
 */
public class ConfigEnumTypeHandler extends BaseTypeHandler<ConfigEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, ConfigEnum configEnum, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, configEnum.getKey());
    }

    @Override
    public ConfigEnum getNullableResult(ResultSet resultSet, String s) throws SQLException {
        final int id = resultSet.getInt(s);
        return ConfigEnum.valueOf(id);
    }

    @Override
    public ConfigEnum getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return ConfigEnum.valueOf(resultSet.getInt(i));
    }

    @Override
    public ConfigEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return ConfigEnum.valueOf(callableStatement.getInt(i));
    }
}